<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- 文章分类数据 -->
      <el-table :data="cateList" stripe border style="width: 100%">
        <el-table-column type="index" label="序号" width="50"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" @click="showEditDialog(row.id)">修改</el-button>
            <el-button type="danger" size="mini" @click="delCate(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加文章分类的对话框 -->
    <el-dialog title="添加文章分类" :visible.sync="addVisible" width="35%" @close="$refs.addFormRef.resetFields()">
      <!-- 表单区域 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="addCate">添 加</el-button>
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 修改文章分类的对话框 -->
    <el-dialog title="修改文章分类" :visible.sync="editVisible" width="35%" @close="$refs.editForm.resetFields()">
      <!-- 表单区域 -->
      <el-form :model="editForm" :rules="addFormRules" ref="editForm" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="editCate">修 改</el-button>
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      // 保存文章分类的列表
      cateList: [],
      // 控制添加对话框的显示与隐藏
      addVisible: false,
      // 控制修改对话框的显示与隐藏
      editVisible: false,
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 修改表单的数据对象
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addFormRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须1-10位非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[A-z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 初始化文章分类数据
    this.initCateList()
  },
  methods: {
    // 删除文章分类
    delCate(id) {
      this.$confirm('是否删除该文章分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete('/my/cate/del', {
            params: { id }
          })
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
          await this.initCateList()
        })
        .catch(() => {})
    },
    // 修改文章分类
    editCate() {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return // 校验失败
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 关闭对话框
        this.editVisible = false
        // 重新获取数据
        await this.initCateList()
      })
    },
    // 展示编辑的对话框
    async showEditDialog(id) {
      const { data: res } = await this.$http.get('/my/cate/info', {
        params: {
          id
        }
      })
      console.log(res)
      if (res.code === 0) {
        this.editForm = res.data
      }
      // 展示对话框
      this.editVisible = true
    },
    // 添加文章分类
    addCate() {
      // 先表单预验证
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return // 校验未通过
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.addVisible = false
        await this.initCateList()
      })
    },
    // 获取文章分类数据
    async initCateList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      console.log(res)
      if (res.code === 0) {
        this.cateList = res.data
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
